set more off

*** DiD dataset
use "$datain\glss_combined.dta", clear

//late sample
format mindate maxdate %td
gen earlycategory=1 if date>=reformdate & mindate>=reformdate & glss==6
replace earlycategory=1 if date5>=reformdate5 & mindate5>=reformdate5 & glss==5


//eligible sample in GLSS5
bysort dist_rural: egen temp=mean(treat) if glss==5
gen eligible5=0 if glss==5
replace eligible5=1 if temp>0 & temp<1 & temp!=.


//anticipation sample
gen anticipation=1 if glss==6 
replace anticipation=0 if date>reformdate-20 & date<=reformdate //exclude households within 10-day radius of treatment status definition, which stands at datereform-10
replace anticipation=1 if glss==5
replace anticipation=0 if glss==5 & date5>reformdate5-20 & date5<=reformdate5

//bandwidths
*2 months (~60 days)
gen b2=0 if glss==6
replace b2=1 if date>reformdate-70 & date<=reformdate+50
*3 months (~90 days)
gen b3=0 if glss==6
replace b3=1 if date>reformdate-100 & date<=reformdate+80
*4 months (~120 days)
gen b4=0 if glss==6
replace b4=1 if date>reformdate-130 & date<=reformdate+110
*6 months (~180 days)
gen b6=0 if glss==6
replace b6=1 if date>reformdate-190 & date<=reformdate+170

//income groups
xtile ginc_r=linccap if glss==6 & rural==1 & (aftercategory==2 | aftercategory==3), n(3)
xtile ginc_u=linccap if glss==6 & urban==1 & (aftercategory==2 | aftercategory==3), n(3)

keep hid d_wood d_charcoal d_lpg lq_coal_hat2 d_qcoal lq_coal_hat lexp_lpg2 ///
									d_elpg lexp_lpg linccap lsize housing_selfowned ///
                                    housing_nrooms lagehead lyeduc unemphead ///
                                    cook_jobweeks cook_female sexhead agrihead  ///
                                    dist_rural rural drer drer2 elec d_road treat glss ///
									d_publictransport aftercategory late50 time6 ///
									earlyc* anticipation latetreat b2 b3 b4 b6 ///
									ginc* late religion lyeduccook eligible5 ///
									region district religion5 ethnicity ethnicity5 ///
									cooking ethnicgroup location totexp_* exp* lexp* treatinten late* month date* reformdate* loc7 
foreach var in d_wood d_charcoal d_lpg d_qcoal lq_coal_hat ///
									d_elpg lexp_lpg linccap lsize housing_selfowned ///
                                    housing_nrooms lagehead lyeduc unemphead ///
                                    cook_jobweeks cook_female sexhead agrihead  ///
                                    dist_rural rural drer elec d_road d_publictransport treat late50 lyeduccook {
										cap drop if `var'==. & glss==5
										cap drop if `var'=="" & glss==5
									}
drop if cook_jobweeks==.
drop if cook_female==.	
drop if elec==99 & rural ==1	
drop if d_road==99 & rural==1							
drop if drer==.		
drop if lyeduccook ==.

//ethnic dummies
tab ethnicgroup, g(eth)

//religion 
recode religion5 (10=1) (1=2) (2=3) (3=3) (4=3) (5=4) (6=4) (7=5) (8=6) (9=7) (96=8)
replace religion=religion5 if glss==5

//DiD
gen treat2=glss==6
gen did=treat*treat2

//clustervar
cap drop clustervar
egen clustervar=group(treat did glss loc7)





save "$datain\glss_DiDsample.dta", replace
